package org.wutopia.labcloud.domain.product.controller;

import lombok.AllArgsConstructor;
import lombok.NonNull;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.wutopia.labcloud.domain.product.controller.vo.input.ProductCreateProductInputVO;
import org.wutopia.labcloud.domain.product.controller.vo.input.ProductModifyProductInputVO;
import org.wutopia.labcloud.domain.product.controller.vo.output.ProductCreateProductOutputVO;
import org.wutopia.labcloud.domain.product.controller.vo.output.ProductGetListOutputVO;
import org.wutopia.labcloud.domain.product.controller.vo.output.ProductGetProducetOutputVO;
import org.wutopia.labcloud.domain.product.service.ProductService;
import org.wutopia.labcloud.library.common.base.CodedMessage;
import org.wutopia.labcloud.library.common.base.CommonResponse;
import org.wutopia.labcloud.library.common.base.PageInputVO;
import org.wutopia.labcloud.library.common.base.PageOutputVO;

import java.io.IOException;
import java.util.List;

@RestController
@AllArgsConstructor
@RequestMapping("/product")

public class ProductController {

    private ProductService productService;


    @PostMapping("/list")
    public ResponseEntity<CodedMessage<PageOutputVO<List<ProductGetListOutputVO>>>> getList(@RequestBody PageInputVO vo) throws IOException {

        PageOutputVO<List<ProductGetListOutputVO>> list = productService.getList(vo);

        return CommonResponse.successWithData(list);
    }

    @GetMapping("/{id}")
    public ResponseEntity<CodedMessage<ProductGetProducetOutputVO>> getProduct(@PathVariable @Validated @NonNull Long id) {


        ProductGetProducetOutputVO productById = productService.getProductById(id);

        return CommonResponse.successWithData(productById);
    }

    @PostMapping("/create")
    public ResponseEntity<CodedMessage<ProductCreateProductOutputVO>> createProduct(@RequestBody ProductCreateProductInputVO vo) {

        ProductCreateProductOutputVO product = productService.createProduct(vo);

        return CommonResponse.successWithData(product);
    }

    @PostMapping("/modify")
    public ResponseEntity<CodedMessage<String>> modify(@RequestBody ProductModifyProductInputVO vo) {

        productService.modify(vo);

        return null;
    }

    @GetMapping("/delete/{id}")
    public ResponseEntity<CodedMessage<String>> delete(@PathVariable Long id) {

        productService.deleteById(id);

        return CommonResponse.success();
    }


}
